// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 7.10.2006 13:40:10
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   SudokuFileFilter.java

package sudoku.model;

import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.filechooser.FileFilter;

public class SudokuFileFilter extends FileFilter {

  public SudokuFileFilter() {
    filters = null;
    description = null;
    fullDescription = null;
    useExtensionsInDescription = true;
    filters = new Hashtable<String, SudokuFileFilter>();
  }

  @Override
  public boolean accept(File f) {
    if (f != null) {
      if (f.isDirectory()) {
        return true;
      }
      String extension = getExtension(f);
      if (extension != null && filters.get(getExtension(f)) != null) {
        return true;
      }
    }
    return false;
  }

  public String getExtension(File f) {
    if (f != null) {
      String filename = f.getName();
      int i = filename.lastIndexOf('.');
      if (i > 0 && i < filename.length() - 1) {
        return filename.substring(i + 1).toLowerCase();
      }
    }
    return null;
  }

  public void addExtension(String extension) {
    if (filters == null) {
      filters = new Hashtable<String, SudokuFileFilter>(5);
    }
    filters.put(extension.toLowerCase(), this);
    fullDescription = null;
  }

  @Override
  public String getDescription() {
    if (fullDescription == null) {
      if (description == null || isExtensionListInDescription()) {
        fullDescription = description != null ? description + " (" : "(";
        Enumeration<String> extensions = filters.keys();
        if (extensions != null) {
          for (fullDescription += "." + extensions.nextElement(); extensions.hasMoreElements(); fullDescription += ", ." + extensions.nextElement()) {
            ;
          }
        }
        fullDescription += ")";
      }
      else {
        fullDescription = description;
      }
    }
    return fullDescription;
  }

  public void setDescription(String description) {
    this.description = description;
    fullDescription = null;
  }

  public boolean isExtensionListInDescription() {
    return useExtensionsInDescription;
  }

  private Hashtable<String, SudokuFileFilter> filters;
  private String description;
  private String fullDescription;
  private boolean useExtensionsInDescription;
}